﻿
CREATE FUNCTION  GetLatestVersionFromProductVersion 
(
	-- Add the parameters for the function here
	@ProductVersion varchar(13)
)
RETURNS varchar(13)
AS
BEGIN
	-- Return the result of the function
	RETURN 
	(

		SELECT TOP 1 REPLACE( V.Version, '.00.','.0.') + '.0'
		FROM SQLServerVersions V 
		WHERE 
			dbo.udf_ItemExtract(1,REPLACE( V.Version, '.00.','.0.'),'.') + '.' +
			dbo.udf_ItemExtract(2,REPLACE( V.Version, '.00.','.0.'),'.')  
			= 
			dbo.udf_ItemExtract(1,@ProductVersion,'.') + '.' + 
			dbo.udf_ItemExtract(2,@ProductVersion,'.') 
	
		Order by V.Version desc
	)

END

